package china.accountinghelper.controls;

import java.util.Map;

import org.afree.chart.AFreeChart;
import org.afree.chart.ChartFactory;
import org.afree.chart.plot.PiePlot;
import org.afree.chart.title.TextTitle;
import org.afree.data.general.DefaultPieDataset;
import org.afree.data.general.PieDataset;
import org.afree.graphics.geom.Font;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.xdroid.chart.ChartView;

public class PieChartView extends ChartView
{

    public PieChartView(Context context, Map<String, Double> pDataSetMap)
    {
        super(context);
        final PieDataset dataset = createDataset(pDataSetMap);
        final AFreeChart chart = createChart(dataset);
        
        setChart(chart);
    }
    
    /**
     * Creates a sample dataset.
     * @return a sample dataset.
     */
    private static PieDataset createDataset(Map<String, Double> pDataSetMap) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        for (String _Key : pDataSetMap.keySet()) {
            dataset.setValue(_Key, pDataSetMap.get(_Key));
            
        }
        
        return dataset;
    }

    /**
     * Creates a chart.
     * @param dataset the dataset.
     * @return a chart.
     */
    private static AFreeChart createChart(PieDataset dataset) {

        AFreeChart chart = ChartFactory.createPieChart(
                "", // chart title
                dataset, // data
                true, // include legend
                true,
                false);
        TextTitle title = chart.getTitle();
//        title.setToolTipText("A title tooltip!");

        PiePlot plot = (PiePlot) chart.getPlot();
//        plot.setLabelFont(new Font("SansSerif", Typeface.NORMAL, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;

    }

    
    
}
